package com.tich.blog.controller;



import com.tich.blog.domain.dto.BlogDTO;
import com.tich.blog.domain.dto.BlogRecommendDTO;
import com.tich.blog.domain.dto.FavoriteBlogDTO;
import com.tich.blog.domain.entity.FavoriteBlog;
import com.tich.blog.domain.vo.BlogRecommendVO;
import com.tich.blog.domain.vo.BlogVO;
import com.tich.blog.service.IBlogService;
import com.tich.blog.service.IFavoriteBlogService;
import com.tich.constant.ResponseMessageConstant;
import com.tich.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王志宇
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/blogs")
@Slf4j
public class BlogController {

    @Autowired
    private IBlogService blogService;
    @Autowired
    private IFavoriteBlogService favoriteBlogService;
    /**
     * 创建帖子
     * @param blogDTO
     * @return
     */
    @PostMapping
    public Result saveBlog(@RequestBody BlogDTO blogDTO) {
        log.info("Saving blog: {}", blogDTO);
        blogService.saveBlog(blogDTO);
        return Result.success(ResponseMessageConstant.SAVE_BLOG_SUCCESS);
    }

    /**
     * 查询帖子
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<BlogVO> queryBlogById(@PathVariable("id") Long id) {
        return Result.success(blogService.queryBlogById(id));
    }

    /**
     * 展示最新发布的帖子
     * @return
     */
    @GetMapping("/recommend")
    public Result<BlogRecommendVO> queryBlogs(@RequestParam("count") Integer count, @RequestParam("cursor")LocalDateTime cursor){
        BlogRecommendDTO blogRecommendDTO = new BlogRecommendDTO();
        blogRecommendDTO.setCount(count);
        blogRecommendDTO.setCursor(cursor);
        return Result.success(blogService.queryBlogs(blogRecommendDTO));
    }

    /**
     * 根据id删除帖子
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteBlogById(@RequestParam("id") Long id) {
        blogService.deleteBlogById(id);
        return Result.success(ResponseMessageConstant.DELETE_SUCCESS);
    }

    /**
     * 收藏帖子
     * @param blogId
     * @return
     */
    @PostMapping("/favorite")
    public Result favoriteBlog(@RequestParam("blogId") Long blogId){
        favoriteBlogService.insert(blogId);
        return Result.success(ResponseMessageConstant.FAVORITE_BLOG_SUCCESS);
    }


    /**
     * 查询收藏帖子接口
     * @param favoriteBlogDTO
     * @return
     */
    @PostMapping("/favorite-blogs")
    public Result<BlogRecommendVO> getFavoriteBlogs(@RequestBody FavoriteBlogDTO favoriteBlogDTO){
        BlogRecommendVO blogRecommendVO = blogService.getFavoriteBlogs(favoriteBlogDTO);
        return Result.success(blogRecommendVO);
    }
}
